package com.xjscrm.console.service.customer.group.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.github.pagehelper.PageHelper;
import com.xjscrm.common.constant.ResponseMsg;
import com.xjscrm.common.entity.WwDevice;
import com.xjscrm.common.entity.WwGroup;
import com.xjscrm.common.entity.WwGroupClassify;
import com.xjscrm.common.entity.WwGroupClassifyRelation;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.common.result.ResponseCode;
import com.xjscrm.common.utils.DateUtil;
import com.xjscrm.common.utils.OrderByUtil;
import com.xjscrm.console.mapper.WwDeviceMapper;
import com.xjscrm.console.mapper.WwGroupClassifyMapper;
import com.xjscrm.console.mapper.WwGroupClassifyRelationMapper;
import com.xjscrm.console.mapper.WwGroupMapper;
import com.xjscrm.console.param.group.*;
import com.xjscrm.console.param.wwDevice.WwDeviceQuery;
import com.xjscrm.console.service.customer.group.WwGroupClassifyService;
import com.xjscrm.console.vo.MyPageInfo;
import com.xjscrm.console.vo.WwDeviceVo;
import com.xjscrm.console.vo.WwGroupClassifyRelationVo;
import com.xjscrm.console.vo.WwGroupClassifyVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class WwGroupClassifyServiceImpl implements WwGroupClassifyService {

    @Autowired
    private WwGroupClassifyMapper wwGroupClassifyMapper;

    @Autowired
    private WwGroupClassifyRelationMapper wwGroupClassifyRelationMapper;

    @Autowired
    private WwDeviceMapper wwDeviceMapper;

    @Autowired
    private WwGroupMapper wwGroupMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addWwGroupClassify(WwGroupClassifyDto wwGroupClassifyDto) {

        if (wwGroupClassifyDto.getMerId() == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if (StringUtils.isBlank(wwGroupClassifyDto.getName())) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "名称不能为空");
        }

        WwGroupClassify wwGroupClassify = new WwGroupClassify();
        BeanUtil.copyProperties(wwGroupClassifyDto, wwGroupClassify);
        int time = DateUtil.getTime();
        long id = IdWorker.getId();
        wwGroupClassify.setId(id);
        wwGroupClassify.setCreateTime(time);
        wwGroupClassify.setUpdateTime(time);

        wwGroupClassifyMapper.insert(wwGroupClassify);

        //添加关系
        if (wwGroupClassifyDto.getWwGroupClassifyRelationDtoList() != null && !wwGroupClassifyDto.getWwGroupClassifyRelationDtoList().isEmpty()) {

            for (WwGroupClassifyRelationDto wwGroupClassifyRelationDto : wwGroupClassifyDto.getWwGroupClassifyRelationDtoList()) {

                WwGroupClassifyRelation wwGroupClassifyRelation = new WwGroupClassifyRelation();
                BeanUtil.copyProperties(wwGroupClassifyRelationDto, wwGroupClassifyRelation);
                wwGroupClassifyRelation.setClassifyId(id);
                wwGroupClassifyRelation.setMerId(wwGroupClassify.getMerId());
                wwGroupClassifyRelationMapper.add(wwGroupClassifyRelation);

            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateWwGroupClassify(WwGroupClassifyDto wwGroupClassifyDto) {

        if (wwGroupClassifyDto.getMerId() == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if (wwGroupClassifyDto.getId() == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.ID_NULL);
        }
        if (StringUtils.isBlank(wwGroupClassifyDto.getName())) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "名称不能为空");
        }

        WwGroupClassify wwGroupClassify = wwGroupClassifyMapper.getById(wwGroupClassifyDto.getId(), wwGroupClassifyDto.getMerId());

        if (wwGroupClassify == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该群组预设不存在");
        }

        BeanUtil.copyProperties(wwGroupClassifyDto, wwGroupClassify);
        wwGroupClassifyMapper.update(wwGroupClassify);

        //删除原 关系
        WwGroupClassifyRelationQuery wwGroupClassifyRelationQuery = new WwGroupClassifyRelationQuery();
        wwGroupClassifyRelationQuery.setMerId(wwGroupClassifyDto.getMerId());
        wwGroupClassifyRelationQuery.setClassifyId(wwGroupClassify.getId());
        List<WwGroupClassifyRelationVo> wwGroupClassifyRelationVoList = wwGroupClassifyRelationMapper.getWwGroupClassifyRelationList(wwGroupClassifyRelationQuery);
        if (wwGroupClassifyRelationVoList != null && !wwGroupClassifyRelationVoList.isEmpty()) {
            wwGroupClassifyRelationVoList.forEach(it -> wwGroupClassifyRelationMapper.delById(it.getId(), it.getMerId()));
        }


        //添加关系
        if (wwGroupClassifyDto.getWwGroupClassifyRelationDtoList() != null && !wwGroupClassifyDto.getWwGroupClassifyRelationDtoList().isEmpty()) {

            for (WwGroupClassifyRelationDto wwGroupClassifyRelationDto : wwGroupClassifyDto.getWwGroupClassifyRelationDtoList()) {

                WwGroupClassifyRelation wwGroupClassifyRelation = new WwGroupClassifyRelation();
                BeanUtil.copyProperties(wwGroupClassifyRelationDto, wwGroupClassifyRelation);
                wwGroupClassifyRelation.setClassifyId(wwGroupClassify.getId());
                wwGroupClassifyRelation.setMerId(wwGroupClassify.getMerId());
                wwGroupClassifyRelationMapper.add(wwGroupClassifyRelation);

            }
        }

    }


    @Override
    public Object getWwGroupClassifyList(WwGroupClassifyQuery wwGroupClassifyQuery) {

        if (wwGroupClassifyQuery.getMerId() == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }

        //通过机器人关键词查询群预设ID
        if (!StringUtils.isBlank(wwGroupClassifyQuery.getDeviceKeyword())) {
            WwDeviceQuery wwDeviceQuery = new WwDeviceQuery();
            wwDeviceQuery.setMerId(wwGroupClassifyQuery.getMerId());
            wwDeviceQuery.setKeyword(wwGroupClassifyQuery.getDeviceKeyword());
            List<WwDeviceVo> wwDeviceVoList = wwDeviceMapper.getWwDeviceList(wwDeviceQuery);
            if (wwDeviceVoList == null || wwDeviceVoList.isEmpty()) {
                return new MyPageInfo<>(null);
            }
            List<String> diviceIds = wwDeviceVoList.stream().map(WwDeviceVo::getDeviceId).distinct().collect(Collectors.toList());

            WwGroupClassifyRelationQuery wwGroupClassifyRelationQuery = new WwGroupClassifyRelationQuery();
            wwGroupClassifyRelationQuery.setMerId(wwGroupClassifyQuery.getMerId());
            wwGroupClassifyRelationQuery.setDeviceIds(diviceIds);
            List<WwGroupClassifyRelationVo> wwGroupClassifyRelationList = wwGroupClassifyRelationMapper.getWwGroupClassifyRelationList(wwGroupClassifyRelationQuery);
            if (wwGroupClassifyRelationList == null || wwGroupClassifyRelationList.isEmpty()) {
                return new MyPageInfo<>(null);
            }
            List<Long> ClassifyIds = wwGroupClassifyRelationList.stream().map(WwGroupClassifyRelationVo::getClassifyId).distinct().collect(Collectors.toList());
            wwGroupClassifyQuery.setClassifyIds(ClassifyIds);
        }

        String orderBy = OrderByUtil.getOrderBy(wwGroupClassifyQuery.getOrderByField(), wwGroupClassifyQuery.isAsc());
        PageHelper.startPage(wwGroupClassifyQuery.getCurrent(), wwGroupClassifyQuery.getSize(), orderBy);

        List<WwGroupClassifyVo> wwGroupClassifyVoList = wwGroupClassifyMapper.getWwGroupClassifyList(wwGroupClassifyQuery);
        if (wwGroupClassifyVoList != null && !wwGroupClassifyVoList.isEmpty()) {

            for (WwGroupClassifyVo wwGroupClassifyVo : wwGroupClassifyVoList) {
                try {
                    WwGroupClassifyRelationQuery wwGroupClassifyRelationQuery = new WwGroupClassifyRelationQuery();
                    wwGroupClassifyRelationQuery.setClassifyId(wwGroupClassifyVo.getId());
                    wwGroupClassifyRelationQuery.setMerId(wwGroupClassifyQuery.getMerId());
                    List<WwGroupClassifyRelationVo> wwGroupClassifyRelationList = wwGroupClassifyRelationMapper.getWwGroupClassifyRelationList(wwGroupClassifyRelationQuery);
                    if (wwGroupClassifyRelationList != null && !wwGroupClassifyRelationList.isEmpty()) {

                        for (WwGroupClassifyRelationVo wwGroupClassifyRelationVo : wwGroupClassifyRelationList) {
                            try {
                                //设置机器人昵称
                                WwDevice wwDevice = wwDeviceMapper.getByDeviceId(wwGroupClassifyRelationVo.getDeviceId(), wwGroupClassifyVo.getMerId());
                                if (wwDevice != null) {
                                    wwGroupClassifyRelationVo.setDeviceName(wwDevice.getDeviceName());
                                    wwGroupClassifyRelationVo.setDeviceUniqueId(wwDevice.getId());
                                    wwGroupClassifyRelationVo.setDeviceHeadImageUrl(wwDevice.getHeadImgUrl());
                                }
                                //设置群名
                                WwGroup wwGroup = wwGroupMapper.getByGroupWxid(wwGroupClassifyRelationVo.getGroupWxid(), wwGroupClassifyRelationVo.getDeviceId(), wwGroupClassifyVo.getMerId());
                                if (wwGroup != null) {
                                    wwGroupClassifyRelationVo.setGroupName(wwGroup.getGroupName());
                                    wwGroupClassifyRelationVo.setGroupId(wwGroup.getId());
                                }


                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }


                        wwGroupClassifyVo.setWwGroupClassifyRelationVoList(wwGroupClassifyRelationList);

                        //获取机器人昵称列表
                        List<String> DeviceIds = wwGroupClassifyRelationList.stream().map(WwGroupClassifyRelationVo::getDeviceId).distinct().collect(Collectors.toList());
                        WwDeviceQuery wwDeviceQuery = new WwDeviceQuery();
                        wwDeviceQuery.setMerId(wwGroupClassifyVo.getMerId());
                        wwDeviceQuery.setDeviceIds(DeviceIds);
                        List<WwDeviceVo> wwDeviceVoList = wwDeviceMapper.getWwDeviceList(wwDeviceQuery);
                        if (wwDeviceVoList != null && !wwDeviceVoList.isEmpty()) {
                            wwGroupClassifyVo.setWwDeviceVoList(wwDeviceVoList);
                        }

                        //获取群昵称列表
//                        List<String> groupWxids = wwGroupClassifyRelationList.stream().map(WwGroupClassifyRelationVo::getGroupWxid).distinct().collect(Collectors.toList());
//                        WwGroupQuery wwGroupQuery = new WwGroupQuery();
//                        wwGroupQuery.setMerId(wwGroupClassifyVo.getMerId());
//                        wwGroupQuery.setGroupWxids(groupWxids);
//                        wwGroupQuery.setDeviceIds(DeviceIds);
//                        List<WwGroupVo> wwGroupVoList = wwGroupMapper.getWwGroupList(wwGroupQuery);
//                        if (wwGroupVoList != null && !wwGroupVoList.isEmpty()) {
//                            wwGroupClassifyVo.setWwGroupVoList(wwGroupVoList);
//                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }

        return new MyPageInfo<>(wwGroupClassifyVoList);
    }


    @Override
    public Object getWwGroupClassifyDetail(WwGroupClassifyDto wwGroupClassifyDto) {

        if (wwGroupClassifyDto.getMerId() == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if (wwGroupClassifyDto.getId() == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.ID_NULL);
        }

        WwGroupClassify wwGroupClassify = wwGroupClassifyMapper.getById(wwGroupClassifyDto.getId(), wwGroupClassifyDto.getMerId());

        if (wwGroupClassify == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该群组预设不存在");
        }

        WwGroupClassifyVo wwGroupClassifyVo = new WwGroupClassifyVo();
        BeanUtil.copyProperties(wwGroupClassify, wwGroupClassifyVo);

        WwGroupClassifyRelationQuery wwGroupClassifyRelationQuery = new WwGroupClassifyRelationQuery();
        wwGroupClassifyRelationQuery.setClassifyId(wwGroupClassifyVo.getId());
        wwGroupClassifyRelationQuery.setMerId(wwGroupClassifyVo.getMerId());
        List<WwGroupClassifyRelationVo> wwGroupClassifyRelationList = wwGroupClassifyRelationMapper.getWwGroupClassifyRelationList(wwGroupClassifyRelationQuery);
        if (wwGroupClassifyRelationList != null && !wwGroupClassifyRelationList.isEmpty()) {

            for (WwGroupClassifyRelationVo wwGroupClassifyRelationVo : wwGroupClassifyRelationList) {
                try {
                    //设置机器人昵称
                    WwDevice wwDevice = wwDeviceMapper.getByDeviceId(wwGroupClassifyRelationVo.getDeviceId(), wwGroupClassifyVo.getMerId());
                    if (wwDevice != null) {
                        wwGroupClassifyRelationVo.setDeviceName(wwDevice.getDeviceName());
                        wwGroupClassifyRelationVo.setDeviceUniqueId(wwDevice.getId());
                        wwGroupClassifyRelationVo.setDeviceHeadImageUrl(wwDevice.getHeadImgUrl());
                    }
                    //设置群名
                    WwGroup wwGroup = wwGroupMapper.getByGroupWxid(wwGroupClassifyRelationVo.getGroupWxid(), wwGroupClassifyRelationVo.getDeviceId(), wwGroupClassifyVo.getMerId());
                    if (wwGroup != null) {
                        wwGroupClassifyRelationVo.setGroupName(wwGroup.getGroupName());
                        wwGroupClassifyRelationVo.setGroupId(wwGroup.getId());
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            wwGroupClassifyVo.setWwGroupClassifyRelationVoList(wwGroupClassifyRelationList);
        }

        return wwGroupClassifyVo;
    }

    @Override
    public void delWwGroupClassify(BatchDelWwGroupClassifyDto batchDelWwGroupClassifyDto) {
        if (batchDelWwGroupClassifyDto.getMerId() == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if (batchDelWwGroupClassifyDto.getId() == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.ID_NULL);
        }

        WwGroupClassify wwGroupClassify = wwGroupClassifyMapper.getById(batchDelWwGroupClassifyDto.getId(), batchDelWwGroupClassifyDto.getMerId());

        if (wwGroupClassify == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该群组预设不存在");
        }

        //删除原 关系
        WwGroupClassifyRelationQuery wwGroupClassifyRelationQuery = new WwGroupClassifyRelationQuery();
        wwGroupClassifyRelationQuery.setMerId(wwGroupClassify.getMerId());
        wwGroupClassifyRelationQuery.setClassifyId(wwGroupClassify.getId());
        List<WwGroupClassifyRelationVo> wwGroupClassifyRelationVoList = wwGroupClassifyRelationMapper.getWwGroupClassifyRelationList(wwGroupClassifyRelationQuery);
        if (wwGroupClassifyRelationVoList != null && !wwGroupClassifyRelationVoList.isEmpty()) {
            wwGroupClassifyRelationVoList.forEach(it -> wwGroupClassifyRelationMapper.delById(it.getId(), it.getMerId()));
        }

        //删除群组
        wwGroupClassifyMapper.delById(wwGroupClassify.getId(), wwGroupClassify.getMerId());


    }
}
